package com.wars2d.client;

import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;

public class Level {
	
	StaticObjectRenderer mRenderer = null;
	
	Stage mLevelStage = new Stage(Global.SCREEN_WIDTH, Global.SCREEN_HEIGHT, false);
	
	public static final int ID_GROUND 	= 0x0000ff;
	public static final int ID_AIR 		= 0x00ff00;
	public static final int ID_PLAYER 	= 0xff0000;
	
	public void load(FileHandle mapFile) {
		TextureAtlas mAtlas = new TextureAtlas(Gdx.files.internal("data/level1/pack"));
		
		mRenderer.addTextureRegion(ID_GROUND, mAtlas.findRegion(String.valueOf(ID_GROUND)));
		mRenderer.addTextureRegion(ID_AIR, mAtlas.findRegion(String.valueOf(ID_AIR)));
		mRenderer.addTextureRegion(ID_PLAYER, mAtlas.findRegion(String.valueOf(ID_PLAYER)));
		
		Pixmap mMap = new Pixmap(mapFile);
		
		int mapWidth 	= mMap.getWidth();
		int mapHeight	= mMap.getHeight();
		
		Format pixelFormat = mMap.getFormat();
		
		if (pixelFormat != Format.RGB888) {
			throw new RuntimeException("format must be RGB888");
		}
		
		for (int i = 0; i < mapHeight; ++i) {
			for (int j = 0; j < mapWidth; ++j) {
				int pixel = mMap.getPixel(j, i);
				
				switch (pixel) {
				case ID_GROUND:
					mLevelStage.addActor(new ActorGround(j, i));
					break;
					
				case ID_AIR:
					mLevelStage.addActor(new AirActor(j, i));
				}
			}
		}
	}
}
